home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / tde40.zip / main.c < prev    next >
C/C++ Source or Header  |  1994-06-05  |  23KB  |  799 lines

  1. /*******************  start of original comments  ********************/
  2. /*
  3.  * Written by Douglas Thomson (1989/1990)
  4.  *
  5.  * This source code is released into the public domain.
  6.  */
  7.  
  8. /*
  9.  * Name:    dte - Doug's Text Editor program - hardware dependent module
  10.  * Purpose: This file contains all the code that needs to be different on
  11.  *           different hardware.
  12.  * File:    hwibm.c
  13.  * Author:  Douglas Thomson
  14.  * System:  This particular version is for the IBM PC and close compatibles.
  15.  *           It write directly to video RAM, so it is faster than other
  16.  *           techniques, but will cause "snow" on most CGA cards. See the
  17.  *           file "hwibmcga.c" for a version that avoids snow.
  18.  *          The compiler is Turbo C 2.0, using one of the large data memory
  19.  *           models.
  20.  * Date:    October 10, 1989
  21.  * Notes:   This module has been kept as small as possible, to facilitate
  22.  *           porting between different systems.
  23.  */
  24. /*********************  end of original comments   ********************/
  25.  
  26.  
  27. /*
  28.  * These routines were rewritten for Microsoft C.  They are pretty much system
  29.  * dependent and pretty much Microsoft C dependent.  I also renamed this file
  30.  * "main.c" - easier to find the main function.
  31.  *
  32.  * New editor name:  TDE, the Thomson-Davis Editor.
  33.  * Author:           Frank Davis
  34.  * Date:             June 5, 1991, version 1.0
  35.  * Date:             July 29, 1991, version 1.1
  36.  * Date:             October 5, 1991, version 1.2
  37.  * Date:             January 20, 1992, version 1.3
  38.  * Date:             February 17, 1992, version 1.4
  39.  * Date:             April 1, 1992, version 1.5
  40.  * Date:             June 5, 1992, version 2.0
  41.  * Date:             October 31, 1992, version 2.1
  42.  * Date:             April 1, 1993, version 2.2
  43.  * Date:             June 5, 1993, version 3.0
  44.  * Date:             August 29, 1993, version 3.1
  45.  * Date:             November 13, 1993, version 3.2
  46.  * Date:             June 5, 1994, version 4.0
  47.  *
  48.  * This modification of Douglas Thomson's code is released into the
  49.  * public domain, Frank Davis.  You may distribute it freely.
  50.  */
  51.  
  52.  
  53. char *greatest_composer_ever = "W. A. Mozart, 1756-1791";
  54.  
  55.  
  56. #include "tdestr.h"             /* tde types */
  57. #include "common.h"
  58. #include "define.h"
  59. #include "help.h"
  60. #include "tdefunc.h"
  61.  
  62.  
  63. #include <fcntl.h>              /* open flags */
  64. #if defined( __UNIX__ )
  65.  #include <signal.h>            /* unix critical errors */
  66. #else
  67.  #include <dos.h>                /* for renaming files */
  68.  #include <bios.h>               /* for direct BIOS keyboard input */
  69.  #include <io.h>                 /* for file attribute code */
  70.  #if defined( __MSC__ )
  71.    #include <errno.h>
  72.    #include <sys\types.h>       /* S_IWRITE etc */
  73.  #endif
  74.  #include <sys\stat.h>           /* S_IWRITE etc */
  75. #endif
  76.  
  77. #if defined( __MSC__ )
  78.  void (interrupt FAR *old_control_c)( ); /* variable for old CNTL-C */
  79.  void (interrupt FAR *old_int1b)( );     /* variable for old int 1b */
  80. #endif
  81.  
  82.  
  83. /*
  84.  * original control-break checking flag
  85.  */
  86. static int s_cbrk;
  87.  
  88.  
  89. /*
  90.  * Name:    main
  91.  * Purpose: To do any system dependent command line argument processing,
  92.  *           and then call the main editor function.
  93.  * Date:    October 10, 1989
  94.  * Passed:  argc:   number of command line arguments
  95.  *          argv:   text of command line arguments
  96.  */
  97. int  main( int argc, char *argv[] )
  98. {
  99. #if defined( __MSC__ )
  100.    union REGS inregs, outregs;
  101. #endif
  102.  
  103.    g_status.found_first = FALSE;
  104.    g_status.arg         = 1;
  105.    g_status.argc        = argc;
  106.    g_status.argv        = argv;
  107.  
  108. #if defined( __UNIX__ )
  109.  
  110.    /*
  111.     * unix signals are kinda analagous to DOS critical errors.
  112.     */
  113. /*
  114.    signal( SIGABRT,   crit_err_handler );
  115.    signal( SIGALRM,   crit_err_handler );
  116.    signal( SIGCHLD,   crit_err_handler );
  117.    signal( SIGCONT,   crit_err_handler );
  118.    signal( SIGFPE,    crit_err_handler );
  119.    signal( SIGHUP,    crit_err_handler );
  120.    signal( SIGILL,    crit_err_handler );
  121.    signal( SIGINT,    crit_err_handler );
  122.    signal( SIGIO,     crit_err_handler );
  123.    signal( SIGIOT,    crit_err_handler );
  124.    signal( SIGKILL,   crit_err_handler );
  125.    signal( SIGPIPE,   crit_err_handler );
  126.    signal( SIGPOLL,   crit_err_handler );
  127.    signal( SIGPWR,    crit_err_handler );
  128.    signal( SIGQUIT,   crit_err_handler );
  129.    signal( SIGSEGV,   crit_err_handler );
  130.    signal( SIGSTOP,   crit_err_handler );
  131.    signal( SIGTERM,   crit_err_handler );
  132.    signal( SIGTRAP,   crit_err_handler );
  133.    signal( SIGTSTP,   crit_err_handler );
  134.    signal( SIGTTIN,   crit_err_handler );
  135.    signal( SIGTTOU,   crit_err_handler );
  136.    signal( SIGURG,    crit_err_handler );
  137.    signal( SIGUSR1,   crit_err_handler );
  138.    signal( SIGUSR2,   crit_err_handler );
  139.    signal( SIGVTALRM, crit_err_handler );
  140.    signal( SIGWINCH,  crit_err_handler );
  141.    signal( SIGXCPU,   crit_err_handler );
  142.    signal( SIGXFSZ,   crit_err_handler );
  143. */
  144. #else
  145.    /*
  146.     * trap control-break to make it harmless, and turn checking off.
  147.     *   trap control-C to make it harmless.
  148.     */
  149. # if defined( __MSC__ )
  150.    inregs.h.ah = 0x33;
  151.    inregs.h.al = 0;
  152.    intdos( &inregs, &outregs );
  153.    s_cbrk = outregs.h.dl;
  154.    old_control_c = _dos_getvect( (unsigned)0x23 );
  155.    _dos_setvect( 0x23, harmless );
  156.    old_int1b = _dos_getvect( (unsigned)0x1b );
  157.    _dos_setvect( 0x1b, ctrl_break );
  158.    inregs.h.ah = 0x33;
  159.    inregs.h.al = 1;
  160.    inregs.h.dl = 0;
  161.    intdos( &inregs, &outregs );
  162. # else
  163.    s_cbrk = getcbrk( );
  164.    ctrlbrk( harmless );
  165.    setcbrk( 0 );
  166. # endif
  167.  
  168.    /*
  169.     * now, install and initialize our simple Critical Error Handler.
  170.     */
  171.    install_ceh( &ceh );
  172. #endif
  173.  
  174.    ceh.flag = OK;
  175.  
  176.    if (initialize( ) != ERROR)
  177.       editor( );
  178.    terminate( );
  179.    return( 0 );
  180. }
  181.  
  182.  
  183. /*
  184.  * Name:    error
  185.  * Purpose: To report an error, and usually make the user type <ESC> before
  186.  *           continuing.
  187.  * Date:    June 5, 1991
  188.  * Passed:  kind:   an indication of how serious the error was:
  189.  *                      WARNING: continue after pressing a key
  190.  *                      FATAL:   abort the editor
  191.  *          line:    line to display message
  192.  *          message: string to be printed
  193.  * Notes:   Show user the message and ask for a key if needed.
  194.  */
  195. void error( int kind, int line, char *message )
  196. {
  197. char buff[MAX_COLS+2];          /* somewhere to store error before printing */
  198. #if defined( __UNIX__ )
  199.  chtype display_buff[MAX_COLS+2];       /* chtype is defined in curses.h */
  200. #else
  201.  char display_buff[(MAX_COLS+2)*2];
  202. #endif
  203.  
  204.    /*
  205.     * tell the user what kind of an error it is
  206.     */
  207.    switch (kind) {
  208.       case FATAL:
  209.          /*
  210.           * fatal error
  211.           */
  212.          assert( strlen( main1 ) < (size_t)g_display.ncols );
  213.          strcpy( buff, main1 );
  214.          break;
  215.      case WARNING:
  216.      default:
  217.          /*
  218.           * warning
  219.           */
  220.          assert( strlen( main2 ) < (size_t)g_display.ncols );
  221.          strcpy( buff, main2 );
  222.          break;
  223.    }
  224.  
  225.    /*
  226.     * prepare the error message itself
  227.     */
  228.    strcat( buff, message );
  229.  
  230.    /*
  231.     * tell the user how to continue editing if necessary
  232.     */
  233.    if (kind == WARNING)
  234.       /*
  235.        * press a key
  236.        */
  237.       strcat( buff, main3 );
  238.  
  239.    /*
  240.     * output the error message
  241.     */
  242.    save_screen_line( 0, line, display_buff );
  243.    set_prompt( buff, line );
  244.  
  245.    if (kind == FATAL) {
  246.       /*
  247.        * no point in making the user type <ESC>, since the program is
  248.        *  about to abort anyway...
  249.        */
  250.       terminate( );
  251.       exit( 1 );
  252.    }
  253.  
  254.    getkey( );
  255.    restore_screen_line( 0, line, display_buff );
  256.    if (g_status.wrapped) {
  257.       g_status.wrapped = FALSE;
  258.       show_search_message( CLR_SEARCH, g_display.mode_color );
  259.    }
  260. }
  261.  
  262.  
  263. /*
  264.  * Name:    harmless
  265.  * Purpose: Do nothing when control-C is pressed
  266.  * Date:    June 5, 1991
  267.  * Notes:   Interrupt 23, the Control-C handler, is a MS DOS system function.
  268.  *            Since we want to use Control-C as a regular function key,
  269.  *            let's do absolutely nothi